package com.ssm.controller;

import com.ssm.pojo.Student;
import com.ssm.pojo.Task;
import com.ssm.service.StudentService;
import com.ssm.vo.TaskVo;
import com.ssm.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    //注册用户功能
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(Student student,HttpSession session){
        //判断用户名和密码是否存在
       Integer isExist = studentService.isExist(student);
       if(isExist==null){
           //注册用户
           int i = studentService.addStudent(student);
           //把自动生成的stuId放入session中，可在整个会话中使用
           session.setAttribute("stuId",student.getStuId());
           if(i>=1){
               return "login";
           }else {
               return "register";
           }
       }else {
           return "register";
       }
    }

    //用户登陆
    @RequestMapping("/login")
    public String login(String userName, String passWord, String user, HttpSession session){
        if(user.equals("普通用户")){
            //普通用户登陆
            //学号是唯一的，可以根据学号，查询登陆用户的所有信息
            Integer i = studentService.StudentLogin(userName,passWord);
            if(i==null){
                return "login";
            }else {
                //当登陆成功后，通过获取的学号，来查询该用户的所有信息，然后存入到session中，可以在各个页面进行调用
                Student student = studentService.selectStudent(userName);
//                System.out.println(stuId);
                //通过在添加新任务中，获取该用户的id，最后把用户的id添加到任务列表中，以被辨识哪个用户登陆，
                //发布任务，实现多对一，最终用户发布，致每个任务显示，都是该用户的信息，即每个不同的用户发布，所显示
                //谁发布的谁显示，显示操作，是根据 多表查询
                session.setAttribute("stuId",student.getStuId());
                session.setAttribute("stuNumber",userName);
                session.setAttribute("stuName",student.getStuName());
                session.setAttribute("passWord",student.getPassWord());
                session.setAttribute("sex",student.getSex());
                session.setAttribute("buildNumber",student.getBuildNumber());
                session.setAttribute("money",student.getMoney());
                session.setAttribute("tel",student.getTel());
                //该下列session的值为了在个人中心进行显示
                //当用户登陆成功后，跳转到用户界面显示
                return "redirect:/resources/loginindex.jsp";
                //return "redirect:/tasks";
            }
        }else if (user.equals("管理员")){
            Integer i = studentService.AdminLogin(userName,passWord);
            if(i==null){
                return "login";
            }else {
                session.setAttribute("userName",userName);
                return "redirect:/resources/adminindex.jsp";
            }
        }
        return "";
    }

    //查询用户信息
    //登陆成功后，把用户名的学号，传入到个人中心，实现登陆用户的信息信息的显示
    @RequestMapping(value = "selectStudent/{stuNumber}",method = RequestMethod.GET)
    @ResponseBody
    public Student selectStudent(@PathVariable("stuNumber") String stuNumber){
        Student student = studentService.selectStudent(stuNumber);
        return student;
    }


    @RequestMapping("updatePersonMessage")
    public String updatePersonMessage(String stuNumber,String stuName,String passWord,int buildNumber,String tel){
        Integer i = studentService.updatePersonMessage(stuName,passWord,buildNumber,tel,stuNumber);
        if (i>=1){
            return "redirect:/resources/page/util/personalMessage.jsp";
        }else {
            return "";
        }
    }

    //给用户充值
    @RequestMapping(value = "/addMoney",method = RequestMethod.POST)
    public String addMoney(String money,String stuNumber){
        Integer i = studentService.updateMoney(money,stuNumber);
        return "/resources/page/util/personalMessage";
    }
    //查询后台的用户信息
    @RequestMapping("/selectUsers")
    @ResponseBody
    public UserVo selTasks(int page, int limit){
        System.out.println("进入查询");
        UserVo userVo = new UserVo();
        userVo.setCode(0);
        userVo.setMsg("");
        List<Student> users = studentService.selectUsers();
        //截取当前页面需要显示的数据
        //计算出当前页数据开始的索引
        int start = (page - 1) * limit;//开始的索引
        int end = page*limit;//结束的索引
        //获取到可以subList的最大索引
        int count = users.size();
        List<Student> data;
        if(end>count){
            if(count==end-limit){
                data = users.subList(start-limit,count);
            }else {
                data = users.subList(start,count);
            }
        }else{
            data = users.subList(start,end);
        }
        userVo.setCount(count);
        userVo.setData(data);
        return userVo;
    }



    //查询操作
    @RequestMapping("/select")
    @ResponseBody
    public UserVo getUserTypes(Integer stuId,Integer stuNumber, int limit,int page){
        UserVo userVo = new UserVo();
        List<Student> users = null;
        try {
            if(stuId==null){
                //如果查询的buildId为空的话，通过查询楼栋号来查询
                users = studentService.selectByStuNumber(stuNumber);
            }else if (stuNumber==null){
                users = studentService.selectByStuId(stuId);
            }else if (stuId!=null&&stuNumber!=null){
                users = studentService.selectByStuIdAndStuNumber(stuId,stuNumber);
            }else {
                users = studentService.selectUsers();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            userVo.setCode(0);
            userVo.setMsg("");
            //截取当前页面需要显示的数据
            //计算出当前页数据开始的索引
            int start = (page - 1) * limit;//开始的索引
            int end = page*limit;//结束的索引
            //获取到可以subList的最大索引
            int count = users.size();
            List<Student> data;
            if(end>count){
                if(count==end-limit){
                    data = users.subList(start-limit,count);
                }else {
                    data = users.subList(start,count);
                }
            }else{
                data = users.subList(start,end);
            }
            userVo.setCount(count);
            userVo.setData(data);
        }
        return userVo;
    }

}
